package com.chandra.pos.product.service;

import com.chandra.pos.SearchCriteria;
import com.chandra.pos.product.dao.IProductDAO;
import com.chandra.pos.product.dao.entity.Product;
import com.chandra.pos.product.exception.ProductNotFoundException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

/**
 * 
 * Product Service
 * 
 * @author onlinetechvision.com
 * @since 25 Mar 2012
 * @version 1.0.0
 *
 */
@Transactional(readOnly = true)
@Named
public class ProductService implements IProductService {

	// ProductDAO is injected...
    @Inject
    IProductDAO productDAO;
	
	/**
	 * Add Product
	 * 
	 * @param  Product product
	 */
	@Transactional(readOnly = false)
	@Override
	public void addProduct(Product product) {
        productDAO.addProduct(product);
	}

	/**
	 * Delete Product
	 * 
	 * @param  Product product
	 */
	@Transactional(readOnly = false)
	@Override
	public void deleteProduct(Product product) {
        productDAO.deleteProduct(product);
	}
	
	/**
	 * Update Product
	 * 
	 * @param  Product product
	 */
	@Transactional(readOnly = false)
	@Override
	public void updateProduct(Product product) {
        productDAO.updateProduct(product);
	}
	
	/**
	 * Get Product
	 * 
	 * @param  int Product Id
	 */
	@Override
	public Product getProductById(Long id) {
		return productDAO.getProductById(id);
	}

	/**
	 * Get Product List
	 * 
	 */
	@Override
	public Page getProducts(PageRequest posPagination) {
		return productDAO.getProducts(posPagination);
	}

    @Override
    public List<Product> searchProducts(String query) {
        return productDAO.searchProducts(query.toLowerCase());  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Product getProduct(String name) throws ProductNotFoundException {
        return productDAO.getProduct(name.toLowerCase()) ;
    }

    @Override
    public List<Product> search(SearchCriteria searchCriteria) {

        if(searchCriteria.getQuery() !=null)
            return    productDAO.searchProducts("%"+searchCriteria.getQuery().toLowerCase()+"%") ;

        else


        return productDAO.searchProducts(searchCriteria);  //To change body of implemented methods use File | Settings | File Templates.
    }


}
